package com.screx.mcloud.lib.utils

import android.app.Activity
import qsos.lib.base.utils.ToastUtils
import java.security.Key
import javax.crypto.Cipher
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.DESKeySpec

object Base64Util {
    /**
     * ECB解密,不要IV
     * @param key 密钥
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    @Throws(Exception::class)
    fun ees3DecodeECB(activity: Activity, key: String?, data: ByteArray?): ByteArray? {
        return try {
            var deskey: Key? = null
            val secretKey = DESKeySpec(key?.toByteArray())
            val keyFactory: SecretKeyFactory = SecretKeyFactory.getInstance("DES")
            deskey = keyFactory.generateSecret(secretKey)
            val cipher: Cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
            cipher.init(Cipher.DECRYPT_MODE, deskey)
            cipher.doFinal(data)
        } catch (e: Exception) {
            ToastUtils.showToast(activity, "请扫描正确的二维码")
            null
        }
    }

}